package org.jeecg.modules.demo.hd.controller;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.HttpServletRequestUtil;
import org.jeecg.common.vo.RequestVo;
import org.jeecg.modules.demo.hd.entity.AppUserGoldMain;
import org.jeecg.modules.demo.hd.service.IAppUserGoldMainService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.entity.AppUserDeviceLog;
import org.jeecg.modules.system.service.IAppUserDeviceLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 用户金币主表
 * @Author: jeecg-boot
 * @Date: 2025-02-04
 * @Version: V1.0
 */
@Api(tags = "用户金币")
@RestController
@RequestMapping("/hd/appUserGold")
@Slf4j
public class AppUserGoldMainController extends JeecgController<AppUserGoldMain, IAppUserGoldMainService> {
    @Autowired
    private IAppUserGoldMainService appUserGoldMainService;

    @Autowired
    private IAppUserDeviceLogService deviceLogService;

    /**
     * 用户金币列表查询
     *
     * @param appUserGoldMain
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "用户金币列表查询", notes = "用户金币列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppUserGoldMain>> queryPageList(AppUserGoldMain appUserGoldMain,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
//        QueryWrapper<AppUserGoldMain> queryWrapper = QueryGenerator.initQueryWrapper(appUserGoldMain, req.getParameterMap());
//		queryWrapper.gt("gold_num",0);
//		queryWrapper.orderByDesc("update_time");
        LambdaQueryWrapper<AppUserGoldMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.gt(AppUserGoldMain::getGoldNum, 0);
        queryWrapper.orderByDesc(AppUserGoldMain::getGoldNum);
        Page<AppUserGoldMain> page = new Page<AppUserGoldMain>(pageNo, pageSize);
        IPage<AppUserGoldMain> pageList = appUserGoldMainService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "查询用户金币余额", notes = "查询用户金币余额")
    @GetMapping(value = "/queryCurrentGold")
    public Result<Double> queryCurrentGold(HttpServletRequest req) {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 保存设备请求日志
        RequestVo requestVo = HttpServletRequestUtil.getDeviceInfoVo(req);
        AppUserDeviceLog deviceInfoVo = new AppUserDeviceLog();
        BeanUtils.copyProperties(requestVo, deviceInfoVo);
        deviceInfoVo.setUserId(currentUser.getId());
        deviceInfoVo.setRequestUrl("查询用户金币余额");
        deviceLogService.save(deviceInfoVo);

        double aFloat = appUserGoldMainService.queryCurrentGold();
        return Result.OK(aFloat);
    }


}
